package com.swinginwind.owf.leave.action;

import java.io.IOException;
import java.io.PrintWriter;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opensymphony.xwork2.ActionSupport;
import com.swinginwind.owf.leave.service.LeaveService;
import com.swinginwind.owf.leave.vo.LeaveVO;

public class LeaveAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private LeaveService leaveService;
	
	private static Gson gson = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
	
	
	
	private LeaveVO leave;
	
	
	
	
	public String getAllLeaveList() throws IOException
	{
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		writer.write(gson.toJson(getLeaveService().getProcessList()));
		writer.flush();
		writer.close();
		return null;
	}
	
	public String getMyAppLeaveList() throws IOException
	{
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		writer.write(gson.toJson(getLeaveService().getMyAppProcessList("DMJ")));
		writer.flush();
		writer.close();
		return null;
	}
	
	public String getMyToDoLeaveList() throws IOException
	{
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		writer.write(gson.toJson(getLeaveService().getMyToDoProcessList("QSX")));
		writer.flush();
		writer.close();
		return null;
	}
	
	
	public String startLeaveProccess() throws IOException
	{
		String result = "success";
		try
		{
			leaveService.startLeaveProcess(leave);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			result = "error";
		}
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		writer.write(result);
		writer.flush();
		writer.close();
		return null;
	}
	
	
	

	public LeaveService getLeaveService() {
		return leaveService;
	}

	public void setLeaveService(LeaveService leaveService) {
		this.leaveService = leaveService;
	}

	public LeaveVO getLeave() {
		return leave;
	}

	public void setLeave(LeaveVO leave) {
		this.leave = leave;
	}

}
